package requests

import (
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
	"wskyygyso/api/app/requests/validators"
)

type VerifyCodeRequest struct {
	CaptchaId     string `json:"captcha_id,omitempty" valid:"captcha_id"`
	CaptchaAnswer string `json:"captcha_answer,omitempty" valid:"captcha_answer"`
	Phone         string `json:"phone,omitempty" valid:"phone"`
}

func VerifyCodePhone(data interface{}, c *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"phone":          []string{"required", "digits:11"},
		"captcha_id":     []string{"required"},
		"captcha_answer": []string{"required", "digits:6"},
	}

	messages := govalidator.MapData{
		"phone":          []string{"required:必须设置手机号码", "digits:手机必须是11位数"},
		"captcha_id":     []string{"required:图片验证码的 ID 为必填"},
		"captcha_answer": []string{"required:图片验证码的 答案 为必填", "digits:验证码长度必须是6位数"},
	}

	errs := validate(data, rules, messages)
	_data := data.(*VerifyCodeRequest)
	return validators.ValidateCaptcha(_data.CaptchaId, _data.CaptchaAnswer, errs)
}

type VerifyCodeEmailRequest struct {
	CaptchaId     string `json:"captcha_id,omitempty" valid:"captcha_id" `
	CaptchaAnswer string `json:"captcha_answer,omitempty" valid:"captcha_answer"`
	Email         string `json:"email,omitempty" valid:"email"`
}

func VerifyCodeEmail(data interface{}, c *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"captcha_id":     []string{"required"},
		"captcha_answer": []string{"required", "digits:6"},
		"email":          []string{"required", "email", "min:4", "max:30"},
	}

	messages := govalidator.MapData{
		"captcha_id": []string{
			"required:图片验证码的 ID 为必填",
		},
		"captcha_answer": []string{
			"required:图片验证码答案必填",
			"digits:图片验证码长度必须为 6 位的数字",
		},
		"email": []string{
			"required:邮箱必须设置",
			"email:必须是邮箱格式",
			"min:最小长度4",
			"man:最大长度30",
		},
	}
	errs := validate(data, rules, messages)
	_data := data.(*VerifyCodeEmailRequest)
	return validators.ValidateCaptcha(_data.CaptchaId, _data.CaptchaAnswer, errs)
}
